package cn.bandeqing.unipush2.service.impl;

import cn.bandeqing.unipush2.entity.MsgType;
import cn.bandeqing.unipush2.entity.Unipush2;
import cn.bandeqing.unipush2.mapper.Unipush2Mapper;
import cn.bandeqing.unipush2.service.IUnipush2Service;
import cn.bandeqing.unipush2.util.Unipush2Util;
import cn.bandeqing.worker.entity.Worker;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import org.dromara.hutool.core.data.id.IdUtil;
import org.dromara.hutool.core.map.MapUtil;
import org.dromara.hutool.json.JSONUtil;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * unipush2推送 服务层实现。
 *
 * @author fugui
 * @since 2024-03-16
 */
@Service
public class Unipush2ServiceImpl extends ServiceImpl<Unipush2Mapper, Unipush2> implements IUnipush2Service {

    @Override
    @Async
    public void sendDispatchMsg(Long workerId, Long orderId, Long owId, MsgType msgType, Worker worker) {
        String requestId = IdUtil.randomUUID();

        Map<String, Object> data = MapUtil.<String, Object>builder()
                .put("msgType", msgType.getValue())
                .put("orderId", orderId)
                .put("owId", owId)
                .put("type", 0)
                .map();

        String jsonStr = JSONUtil.toJsonStr(data);

        Unipush2 unipush2 = Unipush2.builder()
                .requestId(requestId)
                .workerId(workerId)
                .workerName(worker.getWorkerNickName())
                .pushClientid(worker.getWorkerCid())
                .title(msgType.getTitle())
                .content(msgType.getContent())
                .msgType(msgType)
                .data(jsonStr)
                .build();

        save(unipush2);

        Unipush2Util.sendMsg(msgType.getTitle(), msgType.getContent(), requestId, jsonStr, worker.getWorkerCid());
    }

}
